package com.uenpay.im.ui.old.activity.dynamic;

import com.qiyunxin.android.http.HttpInvokeResultCode;
import com.qiyunxin.android.http.utils.StringUtils;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * @author SL
 * @copyRight http://www.qiyunxin.com
 * 
 */
public class ReplyDynamicsInvokeItem extends BaseHttpInvokeItem {

	public class ReplyDynamicsResult extends HttpInvokeResult {
		public Reply mReply = new Reply();
	}

	public ReplyDynamicsResult getOutPut() {
		return (ReplyDynamicsResult) GetResultObject();
	}

	public ReplyDynamicsInvokeItem(int pcustid, int dynamicid, String content) {
		content = StringUtils.encodeParams(content);
		// http://api.aitiapp.com/Dynamic/addReply?dynamicid=&content=&pid=
		String url = APIConfiguration.getBaseUrl()
				+ "Dynamic/Dynamic/addReply?dynamicid=" + dynamicid
				+ "&content=" + content + "&pcustid=" + pcustid + "&"
				+ APIConfiguration.getCustIdAndToken();
		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		ReplyDynamicsResult result = new ReplyDynamicsResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		if (result.status == HttpInvokeResultCode.RESULT_OK) {
			JSONObject jo = null;
			try {
				jo = json.getJSONObject("data");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			if (jo != null) {
				result.mReply.replyid = jo.optInt("replyid") + "";
				if (jo.has("pcustid")) {
					result.mReply.pcustid = jo.optInt("pcustid");
				}
				if (jo.has("pnickname")) {
					result.mReply.pnickname = jo.optString("pnickname");
				}
				if (jo.has("content")) {
					result.mReply.content = jo.optString("content");
				}
				result.mReply.custid = jo.optString("custid");
			}
		}
		return result;
	}
}
